package com.mxy.batterychargingreminding;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.text.DecimalFormat;

/**
 * 手机当前电池信息展示页
 *
 * @author 马晓勇
 */
public class MainActivity extends AppCompatActivity {

    private BatteryChangeReceiver batteryChangeReceiver = new BatteryChangeReceiver();
    private TextView level;
    private TextView health;
    private TextView state;
    private TextView chargingtype;
    private TextView temperature;
    private DecimalFormat decimalFormat = new DecimalFormat("######0");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerse(this);
        setContentView(R.layout.activity_main);
        initView();

        //启动服务
        Intent intent = new Intent(this, MyService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            this.startForegroundService(intent);
        } else {
            this.startService(intent);
        }

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        this.registerReceiver(batteryChangeReceiver, intentFilter);

    }

    private void initView() {
        //当前电量百分比
        level = findViewById(R.id.level);
        //电池健康状况
        health = findViewById(R.id.health);
        //电池充电状态
        state = findViewById(R.id.state);
        //充电类型  充电器/USB/无线
        chargingtype = findViewById(R.id.chargingtype);
        //当前电池温度
        temperature = findViewById(R.id.temperature);
    }

    /**
     * Android 5.0以上沉浸模式
     */
    public static void immerse(Activity cont) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = cont.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(cont.getResources().getColor(R.color.colorPrimary));
            window.setNavigationBarColor(cont.getResources().getColor(R.color.colorPrimary));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(batteryChangeReceiver);
    }

    private class BatteryChangeReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
                level.setText("当前电量: " + intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0) + "%");
                switch (intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1)) {
                    case BatteryManager.BATTERY_HEALTH_COLD:
                        health.setText("电池健康状况: 温度过低");
                        break;
                    case BatteryManager.BATTERY_HEALTH_DEAD:
                        health.setText("电池健康状况: 没电");
                        break;
                    case BatteryManager.BATTERY_HEALTH_GOOD:
                        health.setText("电池健康状况: 良好");
                        break;
                    case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                        health.setText("电池健康状况: 电压过高");
                        break;
                    case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                        health.setText("电池健康状况: 过热");
                        break;
                    case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                        health.setText("电池健康状况: 状态获取失败");
                        break;
                    case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                        health.setText("电池健康状况: 未知错误");
                        break;
                    default:
                        break;
                }
                switch (intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) {
                    case BatteryManager.BATTERY_STATUS_CHARGING:
                        state.setText("电池状态: 充电中");
                        break;
                    case BatteryManager.BATTERY_STATUS_DISCHARGING:
                        state.setText("电池状态: 放电中");
                        break;
                    case BatteryManager.BATTERY_STATUS_FULL:
                        state.setText("电池状态: 电量已满");
                        break;
                    case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                        state.setText("电池状态: 未充电");
                        break;
                    case BatteryManager.BATTERY_STATUS_UNKNOWN:
                        state.setText("电池状态: 未知状态");
                        break;
                    default:
                        break;
                }
                switch (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)) {
                    case BatteryManager.BATTERY_PLUGGED_AC:
                        chargingtype.setText("当前充电方式: 充电器");
                        break;
                    case BatteryManager.BATTERY_PLUGGED_USB:
                        chargingtype.setText("当前充电方式: USB");
                        break;
                    case BatteryManager.BATTERY_PLUGGED_WIRELESS:
                        chargingtype.setText("当前充电方式: 无线");
                        break;
                    default:
                        chargingtype.setText("当前充电方式: 未充电");
                        break;
                }
                String t = decimalFormat
                        .format((double) (intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10));
                temperature.setText("电池温度: " + t + "度");
            }
        }
    }
}